package com.ruoyi.crm.service.impl;

import java.util.Date;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.crm.domain.AssignRecord;
import com.ruoyi.crm.domain.Business;
import com.ruoyi.crm.mapper.BusinessMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.crm.mapper.ClueMapper;
import com.ruoyi.crm.domain.Clue;
import com.ruoyi.crm.service.IClueService;

import javax.annotation.Resource;

/**
 * 线索管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-04-21
 */
@Service
public class ClueServiceImpl implements IClueService 
{
    @Resource
    private ClueMapper clueMapper;

    /**
     * 查询线索管理
     * 
     * @param id 线索管理主键
     * @return 线索管理
     */
    @Override
    public Clue selectClueById(Long id)
    {
        return clueMapper.selectClueById(id);
    }

    /**
     * 查询线索管理列表
     * 
     * @param clue 线索管理
     * @return 线索管理
     */
    @Override
    public List<Clue> selectClueList(Clue clue)
    {
        return clueMapper.selectClueList(clue);
    }

    /**
     * 新增线索管理
     * 
     * @param clue 线索管理
     * @return 结果
     */
    @Override
    public int insertClue(Clue clue)
    {
        clue.setCreateTime(DateUtils.getNowDate());
        return clueMapper.insertClue(clue);
    }

    /**
     * 修改线索管理
     * 
     * @param clue 线索管理
     * @return 结果
     */
    @Override
    public int updateClue(Clue clue)
    {
        clue.setUpdateTime(DateUtils.getNowDate());
        return clueMapper.updateClue(clue);
    }

    /**
     * 批量删除线索管理
     * 
     * @param ids 需要删除的线索管理主键
     * @return 结果
     */
    @Override
    public int deleteClueByIds(Long[] ids)
    {
        return clueMapper.deleteClueByIds(ids);
    }

    /**
     * 删除线索管理信息
     * 
     * @param id 线索管理主键
     * @return 结果
     */
    @Override
    public int deleteClueById(Long id)
    {
        return clueMapper.deleteClueById(id);
    }

    @Override
    public int upload(List<Clue> list) {
        int count=0;
        for (Clue clue:list){
            clue.setStatus(Clue.Status.WFP.getValue());
            clue.setChannel(DictUtils.getDictValue("clues_item",clue.getChannel()));
            clue.setSex(DictUtils.getDictValue("sys_user_sex",clue.getSex()));
            clue.setSubject(DictUtils.getDictValue("course_subject",clue.getSubject()));
            clue.setLevel(DictUtils.getDictValue("clues_level",clue.getLevel()));
            count+=clueMapper.insertClue(clue);
        }
        return count;
    }

    @Override
    public int fake(Long id) {
        Clue clue=clueMapper.selectClueById(id);
        if (clue.getFalseCount()>=3){
            throw new RuntimeException("伪线索超限");
        }
        clue.setStatus(Clue.Status.WXS.getValue());
        clue.setFalseCount(clue.getFalseCount()+1);
        return clueMapper.updateClue(clue);
    }
@Resource
private AssignRecordServiceImpl assignRecordService;
    @Override
    public int restore(Long id) {
        AssignRecord assignRecord=new AssignRecord();
        assignRecord.setType(AssignRecord.Type.CLUE.name());
        assignRecord.setUpdateBy(SecurityUtils.getUsername());
        assignRecord.setUpdateTime(new Date());
        assignRecord.setAssignId(id);
        return assignRecordService.insertAssignRecord(assignRecord);
    }
@Resource
   private BusinessMapper businessMapper;
    @Override
    public int transform(Long clueId) {
        Clue clue=clueMapper.selectClueById(clueId);
        clue.setStatus(Clue.Status.WFP.getValue());
        clue.setUpdateBy(SecurityUtils.getUsername());
        clue.setUpdateTime(new Date());
        clueMapper.updateClue(clue);

        Business business=new Business();
        business.setStatus(Clue.Status.ZJS.getValue());
        business.setClueId(clueId);
        return businessMapper.insertBusiness(business);
    }
}
